function permute(nums) {
    const result = []

    function backtrack(path, choices) {
       if (path.length === nums.length) {
          result.push([...path])
          return
       }
       for (let i = 0; i < choices.length; i++) {
          path.push(choices[i])
          backtrack(path, choices.slice(0, i).concat(choices.slice(i + 1)))
          path.pop()
       }
    }

    backtrack([], nums)
    return result
 }

 // 示例测试
 console.log(permute([1, 2, 3])) // 输出：[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
 console.log(permute([0, 1])) // 输出：[[0,1],[1,0]]
 console.log(permute([1])) // 输出：[[1]]